# -*- coding: utf-8 -*-
def prime(n):
    nn = range(n , 1 ,-1)

    ppp = []
    while 1:
        ll = []
        if len(nn) > 0:
            p = nn.pop()
            ppp.append(p)
            for x in nn:
                if x % p == 0:
                    continue
                else:
                    ll.append(x)
            nn = ll
        else:
            break
    print
    print u'共计:',len(ppp)

def shaifa(n):
    dn ={}
    p = []
    for x in xrange(n):
        ls = len(dn)
        dn[ls] = 1

    for y in dn:
        if y <= 1:
            dn[y] = 0
            continue
        if dn[y] == 1:
            p.append(y)
        for x in xrange(y + y,n,y):
            dn[x] = 0

##    print p
    print n,u' 共计: ',len(p)
    writep(p)

def writep(l):
    import os
    if os.path.exists('prime.txt',):
        print u'文件存在'
        return
    f = open('prime.txt','w')
    a = ' '.join(map(str,l))
    f.write(str(a))
    f.close()

def readp():
    f = open("prime.txt")
    l = f.readlines()
    f.close

    result = []
    for stra in l:
        a = stra.split(' ')
        result = map(int,a)
    return result
def main():
    n = 10000
##    n = 100
    shaifa(n)
    readp()
##    import time
##    start = time.time()
##    prime(n)
##    print('Execution time: {0} seconds'.format(time.time() - start))

##    start = time.time()
##    shaifa(n)
##    print('Execution time: {0} seconds'.format(time.time() - start))

if __name__ == '__main__':
    import time
    start = time.time()
    main()
    print('Execution time: {0} seconds'.format(time.time() - start))

